package com.indusfo.spc.service.impl;

import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.mapper.FixedDeteDetailMapper;
import com.indusfo.spc.mapper.FixedDeteMapper;
import com.indusfo.spc.pojo.FixedDete;
import com.indusfo.spc.pojo.FixedDeteDetail;
import com.indusfo.spc.service.FixedDeteDetailService;
import com.indusfo.spc.service.FixedDeteService;
import com.indusfo.spc.vo.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Stream;

/**
 * @author louk
 * @date 2019/11/23 17:56
 * @description 固定检测项目service
 */
@Service
public class FixedDeteDetailServiceImpl implements FixedDeteDetailService {
    @Resource
    private FixedDeteDetailMapper fixedDeteDetailMapper;
    @Resource
    private FixedDeteMapper fixedDeteMapper;

    /**
     * 新增固定检测项目明细
     * @param fixedDeteDetailList
     * @return
     */
    @Override
    public JSONObject insertDetailList(List<FixedDeteDetail> fixedDeteDetailList) {
        if(!fixedDeteDetailList.isEmpty()){
            int insertNum = fixedDeteDetailMapper.insertDetailList(fixedDeteDetailList);
            if(insertNum>0){
                return JSONObject.build(JSONObject.SUCCESS,"新增成功",insertNum);
            }else{
                return JSONObject.build(JSONObject.ERROR,"新增失败",0);
            }

        }
        return JSONObject.build(JSONObject.SUCCESS,"新增成功",0);
    }

    /**
     * 查询固定检测项目明细
     * @param fixedDeteDetail
     * @return
     */
    @Override
    public JSONObject queryAllDetailList(FixedDeteDetail fixedDeteDetail) {
        Integer pagesize = fixedDeteDetail.getPagesize();
        Integer pageindex = fixedDeteDetail.getPageindex();
        if(pagesize != null && pageindex != null) {
            fixedDeteDetail.setIncept(pagesize*(pageindex - 1));
        }
        List<FixedDeteDetail> fixedDeteDetailList = fixedDeteDetailMapper.selectAllDetail(fixedDeteDetail);
        Integer projectType = fixedDeteMapper.queryProjectType(fixedDeteDetail.getFixedDeteId());
        if(projectType!=null && !"".equals(projectType) && projectType.intValue()==4){
            fixedDeteDetailList.forEach(FixedDeteDetail -> {
                if ("1".equals(FixedDeteDetail.getDeteValue())) {
                    FixedDeteDetail.setDeteValue("合格");
                } else if ("0".equals(FixedDeteDetail.getDeteValue())) {
                    FixedDeteDetail.setDeteValue("不合格");
                }
            });
        }
        if(!fixedDeteDetailList.isEmpty()){
            int selectNum = fixedDeteDetailMapper.countAllDetail(fixedDeteDetail);
            return JSONObject.oK("查询成功",fixedDeteDetailList,selectNum);
        }
        return JSONObject.oK("查询成功",fixedDeteDetailList,null);
    }

    /**
     * 更新固定检测项目
     * @param fixedDeteDetail
     * @return
     */
    @Override
    public JSONObject updateDetail(FixedDeteDetail fixedDeteDetail) {
        String checkParamResult = checkParam(fixedDeteDetail);
        if(!StringUtils.isEmpty(checkParamResult)){
            return JSONObject.build(JSONObject.ERROR,checkParamResult);
        }
        int updateNum = fixedDeteDetailMapper.updateByPrimaryKey(fixedDeteDetail);
        if(updateNum>0){
            return JSONObject.oK("编辑成功");
        }
        return JSONObject.oK("编辑失败");
    }

    /**
     * 启用停用删除
     * @param fixedDeteDetailIds
     * @param dataState
     * @return
     */
    @Override
    public JSONObject definesDetailList(Integer[] fixedDeteDetailIds, Integer dataState) {
        if(fixedDeteDetailIds==null || fixedDeteDetailIds.length==0){
            throw new ParamsErrorException("未选择检测单");
        }
        if(dataState==null || "".equals(dataState) ){
            throw new ParamsErrorException("未选择状态");
        }
        int defineNum = fixedDeteDetailMapper.defineDetail(fixedDeteDetailIds, dataState);
        if(defineNum>0){
            if(new Integer(1).equals(dataState)){
                return JSONObject.oK("启用成功");
            }
            if(new Integer(2).equals(dataState)){
                return JSONObject.oK("删除成功");
            }
            if(new Integer(3).equals(dataState)){
                return JSONObject.oK("停用成功");
            }


        }
        if(new Integer(1).equals(dataState)){
            return JSONObject.oK("启用成功");
        }
        if(new Integer(2).equals(dataState)){
            return JSONObject.oK("删除成功");
        }
        if(new Integer(3).equals(dataState)){
            return JSONObject.oK("停用成功");
        }
        return null;
    }

    /**
     * 新增固定检测项目明细通过检测检测值数组,
     * @param deteValue
     * @param deteValue
     * @param deteDate
     * @return
     */
    @Override
    public JSONObject insertDetail(String[] deteValue,Integer fixedDeteId,String[] deteDate){
        try{
            List<FixedDeteDetail> fixedDeteDetailList=new ArrayList<>();
            if(fixedDeteId==null || "".equals(fixedDeteId)){
                throw new ParamsErrorException("固定检测项目id未获取到");
            }
            String checkProjectTypeResult = checkProjectType(fixedDeteId, deteValue);
            if(!StringUtils.isEmpty(checkProjectTypeResult)){
                return JSONObject.build(JSONObject.ERROR,checkProjectTypeResult);
            }
            if(deteValue.length==0 || deteValue==null){
                return JSONObject.build(JSONObject.SUCCESS,"新增成功",0);
            }

            for (int i = 0; i < deteValue.length; i++) {
                FixedDeteDetail fixedDeteDetail=new FixedDeteDetail();
                String newDate=deteDate[i];
                try {
                    Date parseDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(newDate);
                    deteDate[i]=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(parseDate);
                } catch (ParseException e) {
                    return JSONObject.build(JSONObject.ERROR,"检测时间格式错误");
                }
                fixedDeteDetail.setDeteValue(deteValue[i]);
                fixedDeteDetail.setFixedDeteId(fixedDeteId);
                if(StringUtils.isEmpty(deteDate[i])){
                    fixedDeteDetail.setDeteDate(getDate());
                }else{
                    fixedDeteDetail.setDeteDate(deteDate[i]);
                }
                fixedDeteDetailList.add(fixedDeteDetail);
            }
            if(fixedDeteDetailList.isEmpty()){
                return JSONObject.oK("新增成功",null,null);
            }
            int insertNum = fixedDeteDetailMapper.insertDetailList(fixedDeteDetailList);
            if(insertNum>0){
                return JSONObject.build(JSONObject.SUCCESS,"新增成功",insertNum);
            }else{
                return JSONObject.build(JSONObject.ERROR,"新增失败",0);
            }
        }catch (GlobalException e){
            return JSONObject.build(JSONObject.ERROR,e.getMessage());
        }

    }

    /**
     * 检查参数
     * @param fixedDeteDetail
     */
    public  String checkParam(FixedDeteDetail fixedDeteDetail){
        //检测值
        String deteValue = fixedDeteDetail.getDeteValue();
        //固定检测项目id
        Integer fixedDeteId = fixedDeteDetail.getFixedDeteId();

        if(StringUtils.isBlank(deteValue)){
            return "检测值不能为空";
        }
        if(fixedDeteId==null || "".equals(fixedDeteId)){
            return "固定检测项目未获取";
        }
        return null;
    }
    public String getDate() {
        Date date = new Date();
        String strDateFormat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        return sdf.format(date);
    }

    public String checkProjectType(Integer fixedDeteId,String[] deteValue){
        Integer projectTypeId=fixedDeteMapper.queryProjectType(fixedDeteId);
        if(projectTypeId==4){
            for (String dv : deteValue) {
                if(!"0".equals(dv) && !"1".equals(dv)){
                    return "项目类型为判定项目的检测值只能为0/1";
                }
            }
        }
        return null;
    }
}
